jQuery(function ($) {
    require.config({
        baseUrl: ctx + '/resource/plugins/echarts4',
        paths: {
            echarts: 'echarts',
            roma: 'theme/roma'
        }
    });
    queryResourceNumber();
    queryUsability();
    queryHealth();

    queryJhjInfo();//交换机基本信息

    queryJhjYbp();//交换机可用性

    qyeryJkdYbp();//交换机健康度
});

//参数集合
var kbpCodeStr = kbpCode.split('_').toString();
var kpiCodeStr = "PM-00-00-000-02," + "FM-00-03-001-01," + "KPI-Weblogic-all-7";

//资源分类数量
function queryResourceNumber() {
    $.ajax({
        url: ctx + "/kpiContrast/queryResourceNumber",
        type: 'post',
        data: {kbpCodeStr: kbpCodeStr, areaFlag: areaFlag},
        success: function (data) {
            var map = JSON.parse(data);
            var hostList = map["host"];//主机
            var dataBaseList = map["database"];//数据库
            var middleWareList = map["middleware"];//中间件

            /*********************************************************************************************/
            var table = "";
            table += '<ul>' +
                '<li class="first" style="width: 20%">分类</li>' +
                '<li style="width: 20%">资源总量</li>';
            if (hostList != null && hostList.length != 0) {
                for (var i = 0; i < hostList.length - 2; i++) {
                    table += '<li style="width: 20%">' + hostList[i].BIZ_TYPE + '</li>';
                }
                table += '<li class="last" style="width: 20%">' + hostList[hostList.length - 2].BIZ_TYPE + '</li></ul>';
                table += '<ul>' +
                    '<li class="first" style="width: 20%">数量</li>' +
                    '<li style="width: 20%">' + hostList[hostList.length - 1].RESOURCECOUNT + '</li>';
                for (var i = 0; i < hostList.length - 2; i++) {
                    table += '<li style="width: 20%">' + hostList[i].RESOURCENUMBER + '</li>';
                }
                table += '<li class="last" style="width: 20%">' + hostList[hostList.length - 2].RESOURCENUMBER + '</li></ul>';
                $("#hostInfo").append(table);
            }
            /*********************************************************************************************/
            var dataBasetable = "";
            dataBasetable += '<ul>' +
                '<li class="first" style="width: 33.3%">分类</li>' +
                '<li style="width: 33.3%">资源总量</li>';
            if (dataBaseList != null && dataBaseList.length != 0) {
                for (var i = 0; i < dataBaseList.length - 2; i++) {
                    dataBasetable += '<li style="width: 33.3%">' + dataBaseList[i].BIZ_TYPE + '</li>';
                }
                dataBasetable += '<li class="last" style="width: 33.3%">' + dataBaseList[dataBaseList.length - 2].BIZ_TYPE + '</li></ul>';
                dataBasetable += '<ul>' +
                    '<li class="first" style="width: 33.3%">数量</li>' +
                    '<li style="width:  33.3%">' + dataBaseList[dataBaseList.length - 1].RESOURCECOUNT + '</li>';
                for (var i = 0; i < dataBaseList.length - 2; i++) {
                    dataBasetable += '<li style="width:  33.3%">' + dataBaseList[i].RESOURCENUMBER + '</li>';
                }
                dataBasetable += '<li class="last" style="width:  33.3%">' + dataBaseList[dataBaseList.length - 2].RESOURCENUMBER + '</li></ul>';
                $("#dataBaseInfo").append(dataBasetable);
            }
            /*********************************************************************************************/
            var middleWaretable = "";
            middleWaretable += '<ul>' +
                '<li class="first" style="width: 33.3%">分类</li>' +
                '<li style="width: 33.3%">资源总量</li>';
            if (middleWareList != null && middleWareList.length != 0) {
                for (var i = 0; i < middleWareList.length - 2; i++) {
                    middleWaretable += '<li style="width: 33.3%">' + middleWareList[i].BIZ_TYPE + '</li>';
                }
                middleWaretable += '<li class="last" style="width: 33.3%">' + middleWareList[middleWareList.length - 2].BIZ_TYPE + '</li></ul>';
                middleWaretable += '<ul>' +
                    '<li class="first" style="width: 33.3%">数量</li>' +
                    '<li style="width:  33.3%">' + middleWareList[middleWareList.length - 1].RESOURCECOUNT + '</li>';
                for (var i = 0; i < middleWareList.length - 2; i++) {
                    middleWaretable += '<li style="width:  33.3%">' + middleWareList[i].RESOURCENUMBER + '</li>';
                }
                middleWaretable += '<li class="last" style="width:  33.3%">' + middleWareList[middleWareList.length - 2].RESOURCENUMBER + '</li></ul>';
                $("#middleWareInfo").append(middleWaretable);
            }
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    })
}

//总可用性
function queryUsability() {
    $.ajax({
        url: ctx + "/kpiContrast/queryUsability",
        type: "post",
        data: {kpiCode: kpiCodeStr, kbpCode: kbpCodeStr},
        success: function (data) {
            var map = JSON.parse(data);
            var hostList = map["host"];//主机
            var dataBaseList = map["database"];//数据库
            var middleWareList = map["middleware"];//中间件
            if (hostList[0] != null && hostList.length != 0 && hostList != '') {
                for (var i = 0; i < hostList.length; i++) {
                    var hostStr = parseInt(hostList[i].SUCCESS_VALUE / hostList[i].COUNT_VALUE * 100);
                    var hostHtml = '<div class="easy-pie-chart percentage" data-percent="' + hostStr + '" ';
                    if (0 <= hostStr && hostStr < 20) {
                        hostHtml += 'data-color="#D15B47"';
                    } else if (20 <= hostStr && hostStr < 80) {
                        hostHtml += 'data-color="#87CEEB"';
                    } else {
                        hostHtml += 'data-color="#87B87F"';
                    }
                    hostHtml += 'style="margin-left: 13%">' +
                        '<span class="percent" style="margin-left: 3%">' + hostStr + '</span>%' +
                        '<h3 style="margin-top: -35%;margin-right: -3%">总可用性</h3 >' +
                        '</div>';
                    $("#usability").append(hostHtml);
                    $('.easy-pie-chart.percentage').each(function () {
                        $(this).easyPieChart({
                            barColor: $(this).data('color'),
                            trackColor: '#EEEEEE',
                            scaleColor: false,
                            lineCap: 'butt',
                            lineWidth: 11,
                            animate: ace.vars['old_ie'] ? false : 1000,
                            size: 150
                        }).css('color', $(this).data('color'));
                    });
                }
            } else {
                var hostHtml = '<div class="easy-pie-chart percentage" style="margin-left: 13%">' +
                    '<span class="percent" style="margin-left: 3%">' + '暂无数据' + '</span>' +
                    '<h3 style="margin-top: -35%;margin-right: -3%">总可用性</h3 >' +
                    '</div>';
                $("#usability").append(hostHtml);
            }

            if (dataBaseList[0] != null && dataBaseList.length != 0 && hostList != '') {
                for (var i = 0; i < dataBaseList.length; i++) {
                    var dataBaseStr = parseInt(dataBaseList[i].SUCCESS_VALUE / dataBaseList[i].COUNT_VALUE * 100);
                    var dataBaseHtml = '<div class="easy-pie-chart percentage" data-percent="' + dataBaseStr + '" ';
                    if (0 <= dataBaseStr && dataBaseStr < 20) {
                        dataBaseHtml += 'data-color="#D15B47"';
                    } else if (20 <= dataBaseStr && dataBaseStr < 80) {
                        dataBaseHtml += 'data-color="#87CEEB"';
                    } else {
                        dataBaseHtml += 'data-color="#87B87F"';
                    }
                    dataBaseHtml += 'style="margin-left: 13%">' +
                        '<span class="percent" style="margin-left: 3%">' + dataBaseStr + '</span>%' +
                        '<h3 style="margin-top: -35%;margin-right: -3%">总可用性</h3 >' +
                        '</div>';
                    $("#dataBaseUsability").append(dataBaseHtml);
                    $('.easy-pie-chart.percentage').each(function () {
                        $(this).easyPieChart({
                            barColor: $(this).data('color'),
                            trackColor: '#EEEEEE',
                            scaleColor: false,
                            lineCap: 'butt',
                            lineWidth: 11,
                            animate: ace.vars['old_ie'] ? false : 1000,
                            size: 150
                        }).css('color', $(this).data('color'));
                    });
                }
            } else {
                var dataBaseHtml = '<div class="easy-pie-chart percentage" style="margin-left: 13%">' +
                    '<span class="percent" style="margin-left: 3%">' + '暂无数据' + '</span>' +
                    '<h3 style="margin-top: -35%;margin-right: -3%">总可用性</h3 >' +
                    '</div>';
                $("#dataBaseUsability").append(dataBaseHtml);
            }

            if (middleWareList[0] != null && middleWareList.length != 0 && middleWareList != '') {
                for (var i = 0; i < middleWareList.length; i++) {
                    var middleWareStr = parseInt(middleWareList[i].SUCCESS_VALUE / middleWareList[i].COUNT_VALUE * 100);
                    var middleWareHtml = '<div class="easy-pie-chart percentage" data-percent="' + middleWareStr + '" ';
                    if (0 <= middleWareStr && middleWareStr < 20) {
                        middleWareHtml += 'data-color="#D15B47"';
                    } else if (20 <= middleWareStr && middleWareStr < 80) {
                        middleWareHtml += 'data-color="#87CEEB"';
                    } else {
                        middleWareHtml += 'data-color="#87B87F"';
                    }
                    middleWareHtml += 'style="margin-left: 13%">' +
                        '<span class="percent" style="margin-left: 3%">' + middleWareStr + '</span>%' +
                        '<h3 style="margin-top: -35%;margin-right: -3%">总可用性</h3 >' +
                        '</div>';
                    $("#middleWareUsability").append(middleWareHtml);
                    $('.easy-pie-chart.percentage').each(function () {
                        $(this).easyPieChart({
                            barColor: $(this).data('color'),
                            trackColor: '#EEEEEE',
                            scaleColor: false,
                            lineCap: 'butt',
                            lineWidth: 11,
                            animate: ace.vars['old_ie'] ? false : 1000,
                            size: 150
                        }).css('color', $(this).data('color'));
                    });
                }
            } else {
                var middleWareHtml = '<div class="easy-pie-chart percentage" style="margin-left: 13%">' +
                    '<span class="percent" style="margin-left: 3%">' + '暂无数据' + '</span>' +
                    '<h3 style="margin-top: -35%;margin-right: -3%">总可用性</h3 >' +
                    '</div>';
                $("#middleWareUsability").append(middleWareHtml);
            }



        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });

}

//健康度
function queryHealth() {
    $.ajax({
        url: ctx + "/kpiContrast/queryHealth",
        type: "post",
        data: {kbpCode: kbpCodeStr, areaFlag: areaFlag},
        success: function (data) {
            var map = JSON.parse(data);
            var hostList = map["host"];//主机
            var dataBaseList = map["database"];//数据库
            var middleWareList = map["middleware"];//中间件

            if (hostList != null && hostList.length != 0) {
                var hostStr = parseInt((hostList[1].RESOURCECOUNT - hostList[0].ALERMCOUNT) / hostList[1].RESOURCECOUNT * 100);
                var hostHtml = '<div class="easy-pie-chart percentage" data-percent="' + hostStr + '" ';
                if (0 <= hostStr && hostStr < 20) {
                    hostHtml += 'data-color="#D15B47"';
                } else if (20 <= hostStr && hostStr < 80) {
                    hostHtml += 'data-color="#87CEEB"';
                } else {
                    hostHtml += 'data-color="#87B87F"';
                }
                hostHtml += 'style="margin-left: 13%">' +
                    '<span class="percent" style="margin-left: 3%">' + hostStr + '</span>%' +
                    '<h3 style="margin-top: -35%;margin-right: -3%">总健康度</h3 >' +
                    '</div>';
                $("#health").append(hostHtml);
            }

            if (dataBaseList != null && dataBaseList.length != 0) {
                var dataBaseStr = parseInt((dataBaseList[1].RESOURCECOUNT - dataBaseList[0].ALERMCOUNT) / dataBaseList[1].RESOURCECOUNT * 100);
                var dataBaseHtml = '<div class="easy-pie-chart percentage" data-percent="' + dataBaseStr + '" ';
                if (0 <= dataBaseStr && dataBaseStr < 20) {
                    dataBaseHtml += 'data-color="#D15B47"';
                } else if (20 <= dataBaseStr && dataBaseStr < 80) {
                    dataBaseHtml += 'data-color="#87CEEB"';
                } else {
                    dataBaseHtml += 'data-color="#87B87F"';
                }
                dataBaseHtml += 'style="margin-left: 13%">' +
                    '<span class="percent" style="margin-left: 3%">' + dataBaseStr + '</span>%' +
                    '<h3 style="margin-top: -35%;margin-right: -3%">总健康度</h3 >' +
                    '</div>';
                $("#dataBaseHealth").append(dataBaseHtml);
            }

            if (middleWareList != null && middleWareList.length != 0) {
                var middleWareStr = parseInt((middleWareList[1].RESOURCECOUNT - middleWareList[0].ALERMCOUNT) / middleWareList[1].RESOURCECOUNT * 100);
                var middleWareHtml = '<div class="easy-pie-chart percentage" data-percent="' + middleWareStr + '" ';
                if (0 <= middleWareStr && middleWareStr < 20) {
                    middleWareHtml += 'data-color="#D15B47"';
                } else if (20 <= middleWareStr && middleWareStr < 80) {
                    middleWareHtml += 'data-color="#87CEEB"';
                } else {
                    middleWareHtml += 'data-color="#87B87F"';
                }
                middleWareHtml += 'style="margin-left: 13%">' +
                    '<span class="percent" style="margin-left: 3%">' + middleWareStr + '</span>%' +
                    '<h3 style="margin-top: -35%;margin-right: -3%">总健康度</h3 >' +
                    '</div>';
                $("#middleWareHealth").append(middleWareHtml);
            }

            $('.easy-pie-chart.percentage').each(function () {
                $(this).easyPieChart({
                    barColor: $(this).data('color'),
                    trackColor: '#EEEEEE',
                    scaleColor: false,
                    lineCap: 'butt',
                    lineWidth: 11,
                    animate: ace.vars['old_ie'] ? false : 1000,
                    size: 150
                }).css('color', $(this).data('color'));
            });
        }
    });

}

/**交换机基本信息*/
function queryJhjInfo() {
    $.ajax({
        url: ctx + "/HostPage/queryTableInfo",
        type: "post",
        async: false,
        data: {CI_TYPE: 'networkdevice', DI_QU: areaFlag},
        success: function (data) {
            var map = JSON.parse(data);
            var list = map.list;
            var list1 = map.list1;
            var tablel = "";
            if (list1 == undefined || list == undefined || list == '' || list1 == '') {
                tablel += "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "分类" + "</li>" +
                    " <li style='width:33.3%'>" + "资源总数" + "</li>" +
                    "<li class='last' style='width:33.3%'>" + "暂无数据" + "</li>" +
                    "</ul>" +
                    "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "数量" + "</li>" +
                    " <li style='width:33.3%'>" + "暂无数据" + "</li>" +
                    "<li class='last' style='width:33.3%'>" + "暂无数据" + "</li>" +
                    "</ul>";
                $("#jhj_info").append(tablel);
            } else {
                jhj = list1[0].ZSL;
                tablel += "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "分类" + "</li>" +
                    " <li style='width:33.3%'>" + "资源总数" + "</li>" +
                    "<li class='last' style='width:33.3%'>" + list[0].CLASSNAME + "</li>" +
                    "</ul>" +
                    "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "数量" + "</li>" +
                    " <li style='width:33.3%'>" + list1[0].ZSL + "</li>" +
                    "<li class='last' style='width:33.3%'>" + list[0].SL + "</li>" +
                    "</ul>";
                $("#jhj_info").append(tablel);
            }
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

/**交换机可用性*/
//PM-00-00-000-02
function queryJhjYbp() {
    $.ajax({
        url: ctx + "/HostPage/queryYbp",
        type: "post",
        data: {CI_TYPE: 'networkdevice', KPI_CODE: "KPI-device-ALL-48", DI_QU: areaFlag},
        success: function (data) {
            var cpuState = JSON.parse(data);
            if (!cpuState) {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='' data-color='#5eadf0'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:40px;'>" + "暂无数据" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "可用性" + "</h3>";
                $("#jhj_ping").append(qq);
            } else {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='jhj_kyx' data-color='#5eadf0'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:40px;'>" + parseInt(cpuState[0].SUCCESS_VALUE / cpuState[0].COUNT_VALUE * 100) + "%" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "可用性" + "</h3>";
                $("#jhj_ping").append(qq);
                $('#jhj_kyx').attr('data-percent', parseInt(cpuState[0].SUCCESS_VALUE / cpuState[0].COUNT_VALUE * 100));
            }
            $('.easy-pie-chart.percentage').each(function () {
                $(this).easyPieChart({
                    barColor: $(this).data('color'),
                    trackColor: '#EEEEEE',
                    scaleColor: false,
                    lineCap: 'butt',
                    lineWidth: 8,
                    animate: ace.vars['old_ie'] ? false : 1000,
                    size: 120
                }).css('color', $(this).data('color'));
            });
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });

}

//交换机健康度
function qyeryJkdYbp() {
    $.ajax({
        url: ctx + "/HostPage/queryJkd",
        type: "post",
        data: {CI_TYPE: 'networkdevice', DI_QU: areaFlag},
        success: function (data) {
            var cpuState = JSON.parse(data);
            if (jhj == undefined) {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='' data-color='#87B87F'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:12px;'>" + "暂无数据" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "健康度" + "</h3>";
                $("#jhj_jkd").append(qq);
            } else {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='jhjjkd' data-color='#87B87F'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:40px;'>" + parseInt((jhj - cpuState[0].EXT_UNIT_ID) / jhj * 100) + "%" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "健康度" + "</h3>";
                $("#jhj_jkd").append(qq);
                $('#jhjjkd').attr('data-percent', parseInt((jhj - cpuState[0].EXT_UNIT_ID) / jhj * 100));
            }
            $('.easy-pie-chart.percentage').each(function () {
                $(this).easyPieChart({
                    barColor: $(this).data('color'),
                    trackColor: '#EEEEEE',
                    scaleColor: false,
                    lineCap: 'butt',
                    lineWidth: 8,
                    animate: ace.vars['old_ie'] ? false : 1000,
                    size: 120
                }).css('color', $(this).data('color'));
            });
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });

}



